package com.wnxy.pms.product.core.controller;


import com.wnxy.pms.common.api.Result;
import com.wnxy.pms.common.api.enums.impl.ResultCode;
import com.wnxy.pms.product.core.dto.OrderPayDto;
import com.wnxy.pms.product.core.exception.BusinessException;
import com.wnxy.pms.product.core.service.IPromotionSeckillService;
import com.wnxy.pms.product.core.vo.OrderVo;
import com.wnxy.pms.product.core.vo.PaymentVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * <p>
 * 秒杀活动表 前端控制器
 * </p>
 *
 * @author Jet
 * @since 2022-06-06
 */
@RestController
@RequestMapping("/promotion-seckill")
@Slf4j
@Api(tags = "秒杀活动")
public class PromotionSeckillController {

    @Autowired
    private IPromotionSeckillService promotionSeckillService;

    /**
     * 秒杀商品列表，选择一个商品，就可以点击抢购
     * 抢购:
     *      生成订单
     *      远程访问订单系统
     *      一个用户，只能购买一个类型的商品
     *      购买商品后，库存需要变化
     * 如何设计库存，支撑高并发访问？
     */

    // 在秒杀产品列表中，点击抢购，后台生成订单，需要先登陆
    // 所以这里需要进行认证的校验，放到网关中实现
    @PostMapping("/auth/order/{promotionId}")
    @ApiOperation("生成订单")
    public Result<OrderVo> submitOrder(
            @PathVariable("promotionId") Integer promotionId,
            HttpServletRequest request){
        String userId = request.getHeader("userId");
        log.info("获取用户ID：{}",userId);
        // 返回用户信息、订单编号
        OrderVo orderVo = promotionSeckillService.submitOrder(userId,promotionId);
        return Result.ok(orderVo);
    }

    @PostMapping("/auth/order/confirm")
    @ApiOperation("确认订单")
    public Result confirmOrder(@RequestBody OrderPayDto orderPayDto){
        ServletRequestAttributes requestAttributes =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String userId = request.getHeader("userId");
        orderPayDto.setUserId(userId);
        //将订单待支付的信息，存储到MQ中。
        try {
            PaymentVo vo = promotionSeckillService.confirmOrder(orderPayDto);
            //返回待支付的订单数据。
            return Result.ok(vo);
        } catch (BusinessException e) {
            return Result.fail(ResultCode.FAILED.getCode(),e.getMessage());
        }
    }

    @PutMapping("/auth/pay")
    @ApiOperation("订单支付")
    public Result pay(@RequestBody OrderPayDto orderDto, HttpServletRequest request){
        String userId = request.getHeader("userId");
        orderDto.setUserId(userId);
        try {
            Integer status = promotionSeckillService.payOrder(orderDto);
            return Result.ok(status);
        } catch (BusinessException e) {
            return Result.fail(ResultCode.FAILED.getCode(),e.getMessage());
        }
    }


}






















